package com.beijiteshop.shop.ui.home.adapter

import android.view.View
import androidx.databinding.ViewDataBinding
import com.beijiteshop.libcommon.utils.setDeleteLine
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.ItemRecProductBinding
import com.beijiteshop.shop.databinding.ItemRushProductBinding
import com.beijiteshop.shop.model.entity.ProductItemEntity

/**
 * 首页商品列表Adapter
 */
class ProductAdapter(data: MutableList<ProductItemEntity>) : BaseMultiItemQuickAdapter<ProductItemEntity,
        BaseDataBindingHolder<ViewDataBinding>>(data),LoadMoreModule{

    init {
        addItemType(ProductItemEntity.TYPE_HEADER_RUSH, R.layout.header_rush_product)
        addItemType(ProductItemEntity.TYPE_HEADER_REC, R.layout.header_rec_product)
        addItemType(ProductItemEntity.TYPE_RUSH_GOODS,R.layout.item_rush_product)
        addItemType(ProductItemEntity.TYPE_COMMON_GOODS,R.layout.item_rec_product)
    }

    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: ProductItemEntity) {
        when(holder.itemViewType){

            ProductItemEntity.TYPE_HEADER_RUSH -> {

            }
-
            ProductItemEntity.TYPE_HEADER_REC -> {

            }

            ProductItemEntity.TYPE_RUSH_GOODS -> {
                val binding: ItemRushProductBinding? = holder.dataBinding as? ItemRushProductBinding
                binding?.let {
                    if (item.data == null) return
                    binding.product = item.data
                    binding.rushProductBar.setUI(item.data.inventory!!.toFloat().toInt(),100,false)
                    binding.rushProductOriginalPriceTv.setDeleteLine()
                    if(item.data.sellOut()){

                        binding.rushProductSaleStateTv.text = context.resources.getString(R.string.sale_out)
                            binding.rushProductSaleStateTv.visibility =  View.VISIBLE
                    }else{
                        binding.rushProductSaleStateTv.visibility =  View.GONE
                    }
                    binding.executePendingBindings()
                }
            }

            ProductItemEntity.TYPE_COMMON_GOODS -> {
                val binding: ItemRecProductBinding? = holder.dataBinding as? ItemRecProductBinding
                binding?.let {
                    if (item.data == null) return
                    binding.product = item.data

                    binding.itemRecProductFlagTv.text = item.data.intro
                    binding.itemRecProductPriceTv.text = item.data.sellingPrice.toString()
                    binding.itemRecProductOriginalPriceTv.setDeleteLine()
                    if(item.data.sellOut()){
                        binding.itemRecProductSaleStateTv.text = context.resources.getString(R.string.sale_out)
                        binding.itemRecProductSaleStateTv.visibility  =  View.VISIBLE
                    }else{
                        binding.itemRecProductSaleStateTv.visibility  = View.GONE
                    }
                    binding.executePendingBindings()
                }
            }
        }
    }
}